package xyz.caijiwang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.caijiwang.entity.Article;
import xyz.caijiwang.entity.Comment;
import xyz.caijiwang.entity.User;
import xyz.caijiwang.entity.res.CommentRes;
import xyz.caijiwang.mapper.ArticleMapper;
import xyz.caijiwang.mapper.CommentMapper;
import xyz.caijiwang.service.ArticleService;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    CommentMapper commentMapper;

    @Override
    public PageInfo<Article> pageGetAllArticle(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleMapper.selectAll();
        PageInfo pageInfo = new PageInfo(articles);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateArticle(Article article) {
        articleMapper.updateByPrimaryKeySelective(article);
    }

    @Override
    public void delArticle(Integer articleid) {
        Article article = new Article();
        article.setArticleid(articleid);
        article.setDelsign(true);
        articleMapper.updateByPrimaryKeySelective(article);
    }

    @Override
    public List<CommentRes> getCommentsByArticle(Integer articleid) {
        List<Comment> comments = commentMapper.selectCommentFatherByArticle(articleid);
        List<CommentRes> list = new ArrayList<>();
        for (Comment comment : comments) {
            Integer commentid = comment.getCommentid();
            CommentRes commentRes = new CommentRes();
            commentRes.setComment(comment.getComment());
            commentRes.setUserid(comment.getUserid());
            commentRes.setArticleid(comment.getArticleid());
            commentRes.setCreattime(comment.getCreattime());
            commentRes.setParentid(0);
            commentRes.setCommentid(commentid);
            List<Comment> listchild = new ArrayList<>();
            List<Comment> finalchildlist = querySubComments(commentid, listchild);
            commentRes.setChildren(finalchildlist);
            list.add(commentRes);
        }
        return list;
    }

    @Override
    public User getUserInfoByParentid(Integer parentid) {
        return commentMapper.getUserInfoByParentid(parentid);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addFatherComment(Comment record) {
        commentMapper.insertSelective(record);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addChildComment(Comment record) {
        commentMapper.insertSelective(record);
    }

    private List<Comment> querySubComments(Integer parentid, List<Comment> listchild) {
        List<Comment> comments = commentMapper.selectCommentChildrenByParentid(parentid);
        if (comments != null) {
            for (Comment comment : comments) {
                listchild.add(comment);
                querySubComments(comment.getCommentid(), listchild);
            }
        } else {
            return listchild;
        }
        return listchild;
    }

}
